package com.ch.dream.logic.main;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;
import cn.goso.utility.NumberTools;

import com.ch.dream.R;
import com.ch.dream.UpdateManager;
import com.ch.dream.activity.main.MainActivity;
import com.ch.dream.asynctask.GetTimeRequestAsyncTask;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.common.CommonData;
import com.ch.dream.tools.AnimationHelper;
import com.ch.dream.tools.DateHelper;
import com.ch.dream.tools.NetTools;
import com.ch.dream.tools.ViewTools;

public class MainLogic{
	
	private Activity activity;
	private TextView tv1;
	private TextView tv2;
	private TextView tv3;
	private TextView tv4;
	private TextView tvNext;
	private Animation animation1;
	private Animation animation2;
	private Animation animation3;
	private Animation animation4;
	private int animTime =1000;
	private boolean isOver =false;
	public MainLogic(MainActivity activity) {
		this.activity = activity;
	}
	public void init() throws Exception{
		if(!NetTools.isNetworkAvailable(activity)){
			ViewTools.showMessage(activity,"提示信息","网络不可用，请检查网络！","确定",new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					int pid = android.os.Process.myPid();
					android.os.Process.killProcess(pid);
				}
			});
			return;
		}
		CommonData.currentActivity =activity;
		activity.setContentView(R.layout.main);
		initViews();
		
		animation1 = AnimationHelper.move(animTime,0,-50);
		animation2 =  AnimationHelper.move(animTime,-50,0);
		animation3 =  AnimationHelper.move(animTime,50,0);
		animation3.setAnimationListener(animationListener);
		animation4 =  AnimationHelper.show(animTime);
		animation4.setAnimationListener(animationListener);
		tv1.startAnimation(animation1);
		tv2.startAnimation(animation2);
		tv3.startAnimation(animation3);
	}
	
	public void initViews(){
		tv1 = (TextView)activity.findViewById(R.id.text1);
		tv2 = (TextView)activity.findViewById(R.id.text2);
		tv3 = (TextView)activity.findViewById(R.id.text3);
		tv4 = (TextView)activity.findViewById(R.id.text4);
		tvNext =tv2;
	}
	
	AnimationListener animationListener = new AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {
			
		}
		
		@Override
		public void onAnimationRepeat(Animation animation) {
			
		}
		
		@Override
		public void onAnimationEnd(Animation animation) {
			if(animation==animation4){
				UpdateManager.update();
			}else if (animation==animation3){
				tv4.setVisibility(View.VISIBLE);
				tv4.startAnimation(animation4);
			}
			
		}
	};
	/**
	 * 判断是否超过一天
	 */
	private boolean isPassDay(String date) throws Exception{
		new GetTimeRequestAsyncTask(activity,new RequestServerListener() {
			@Override
			public void onRequestServerEnd(Object result) {
				if(result == null){
					if(activity == CommonData.currentActivity){
						ViewTools.showMessage(activity, "更新服务器时间错误");
					}
				}
			}
			
			@Override
			public void onRequestServerBegin() {
				
			}
		}).execute();
		int waitMaxTime = CommonData.TIMEOUT;
		while(!CommonData.GETTIMEOVERFLAG){
			if(waitMaxTime>0){
				waitMaxTime = waitMaxTime-500;
				Thread.sleep(500);
			}else{
				break;
			}
		}
		if(CommonData.GETTIMEOVERFLAG){
			CommonData.GETTIMEOVERFLAG =false;
		}
		String cnt = DateHelper.cntTimeDifference(date,CommonData.SERVICETIME, DateHelper.dateFormat, DateHelper.dateFormat);
		int diffHours = NumberTools.stringToInt(cnt);
		if(diffHours>23){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断是否曾经登录
	 * @return
	 */
	private boolean isLogin(){
		SharedPreferences  sp =activity.getSharedPreferences(CommonData.USERINFO,Context.MODE_PRIVATE);
		if(sp!=null){
			String userName = sp.getString("userName","");
			String password = sp.getString("password", "");
			if("".equals(userName) || "".equals(password) ){
				return false;
			}else{
				return true;
			}
		}else{
			return false;
		}
	}
}
